<template>
  <!--基本信息  -->
  <div>
    <FormPanel
      ref="formPanel"
      :list="initForm"
      :cols="2"
      :formType="type"
      :labelWidth="110"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button
        v-if="type !== 'show'"
        :loading="saveLoading"
        @click="saveDialog"
        type="primary"
      >{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { authority } from '@/utils/authMixin';
import { dictionary } from '@/utils/dictMixin';
import { updateCarInfo } from '@/api/cms-api/customerManage/continueManage/record/index';
export default {
  mixins: [authority, dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      projectModelId: this.rowData['projectModelId'],
      projectId: this.rowData['projectId'],
      materialId: this.rowData['materialId'],
      saveLoading: false,
      initForm: this.initFormFun(),
      formValue: {}
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.initForm.forEach(x => {
        x.initialValue = this.rowData[x.fieldName];
      });
    },
    initFormFun() {
      return [
        {
          type: 'INPUT',
          label: '整车编号:',
          fieldName: 'productCode',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '产品名称:',
          fieldName: 'productName',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '车型名称:',
          fieldName: 'modelName',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '数量（台）:',
          fieldName: 'vehicleNum',
          rules: [{ required: true, message: '请填写数量', trigger: 'blur' }, { pattern: /^([1-9]|[1-9]\\d|100)$/, message: '请填1-100的整数', trigger: 'blur' }],
          disabled: false
        },

        {
          type: 'DATE',
          label: '预计交车时间',
          fieldName: 'planDate',
          rules: [{ required: true, message: '请填写预计交车时间', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '其他情况说明:',
          fieldName: 'otherDesc',
          disabled: false,
          rules: [{ max: 100, message: '个其他情况说明长度不能超过100', trigger: 'blur' }]
        }
      ];
    },
    async formChangeHandle(val) {
      this.formValue = val;
      console.log('wewewewe---', val);
      if (this.formValue !== null) {
        this.saveLoading = true;
        let res = {};
        this.formValue = Object.assign({}, { projectId: this.projectId }, this.formValue);
        this.formValue = Object.assign({}, { materialId: this.materialId }, this.formValue);
        if (this.type === 'edit') {
          res = await updateCarInfo(this.formValue, this.projectModelId);
        }
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
        }
      }
    },
    saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>
<style scoped>
.wrapper {
  height: calc(100vh - 110px);
}
</style>
